package test21.homeWork;

import java.io.*;
import java.net.ServerSocket;
import java.net.Socket;

//使用字符流的方式，编写一个客户端程序和服务器端程序
// 客户端发送“name",服务器端接收到后，返回"我是 nova",nova 是你自己的名字
// 客户端发送"hobby",服务器端接收到后，返回“编写java程序"
// 不是这两个问题，回复"你说啥呢”
public class Homework01Server {
    public static void main(String[] args) throws IOException {
        //等待连接
        ServerSocket serverSocket = new ServerSocket(9999);
        System.out.println("服务端，在9999端口监听，等待连接..");
        Socket socket = serverSocket.accept();
        //获取数据
        InputStream inputStream = socket.getInputStream();
        BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream));
        String s = bufferedReader.readLine();
        //判断问题
        String answer = "";
        if ("name".equals(s)) {
            answer = "我是韩顺平";
        } else if("hobby".equals(s)) {
            answer = "编写java程序";
        } else {
            answer = "你说的啥子";
        }
        //回复信息
        OutputStream outputStream = socket.getOutputStream();
        BufferedWriter bufferedWriter = new BufferedWriter(new OutputStreamWriter(outputStream));
        bufferedWriter.write(answer);
        bufferedWriter.newLine();// 插入一个换行符，表示回复内容的结束
        bufferedWriter.flush();//注意需要手动的flush
        //关闭流和socket
        bufferedWriter.close();
        bufferedReader.close();
        socket.close();
        serverSocket.close();
    }
}
